/* $RCSFile: ADNDynSalesliteratureitem.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynSalesliteratureitem.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:49  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

/* Copyrights Christian Surieux 2010 */
/* Class ADNDynSalesliteratureitem */
/* Date 2010-01-10 19:34:29Z */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using CrmSdk;
using System.Globalization;
using System.IO;
using System.Web.Services.Protocols;
using System.Data;

namespace AddonNice.CRMWrapper
{
    /// <summary>
    /// Beware abstract is replaced by wabstract because it is a c# reserved word
    /// </summary>
    public class ADNDynSalesliteratureitem : ADNDynCommonDoc
    {
        /* Internal class for DynTypes */
        protected class Ftor : DFactor
        {
            public Ftor() : base(CrmStr.salesliteratureitem) { }
            public override Type DynType
            { get { return typeof(ADNDynSalesliteratureitem); } }
            public override string DynTypename
            { get { return CrmStr.salesliteratureitem; } }
            public override DynEntity Build()
            { return new ADNDynSalesliteratureitem(); }
            public override DynEntity Build(CRMWrapperBase w,InternalDynamicEntity dEntity,PropertyDescriptorCollection wproperties)
            { return new ADNDynSalesliteratureitem(w,dEntity,wproperties,null); }
            public override DynEntity Build(CRMWrapperBase w,string PrimaryFieldValue)
            { return new ADNDynSalesliteratureitem(w,PrimaryFieldValue); }
            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            { return new ADNDynSalesliteratureitem(w,PrimaryKeyValue); }
        }
        /* Static Ctor to register in DynTypes */
        static ADNDynSalesliteratureitem() { new Ftor(); }

        public override bool HasOwnerProperty
        { get { return false; } }

        public override string SchemaName
        {
           get
           {
               if ( string.IsNullOrEmpty(_SchemaName) )
                    _SchemaName =   CrmStr.salesliteratureitem;
               return _SchemaName;
           }
           set{}
        }

        public override ADNTypeCodes ADNTypeCode
        {   get{ return ADNTypeCodes.SalesLiteratureItem ; }}


        public override int ObjectTypeCode
        {
           get
           {
               if ( _ObjectTypeCode == 0 )
                    _ObjectTypeCode    =   1070;
               return _ObjectTypeCode;
           }
           set{}
        }

        public ADNDynSalesliteratureitem() : base() { }
        public ADNDynSalesliteratureitem(CRMWrapperBase w,string PrimaryFieldValue)
            : base(w,CrmStr.salesliteratureitem)
        {
            if ( PrimaryFieldValue != string.Empty )
                title = PrimaryFieldValue;
        }
        public ADNDynSalesliteratureitem(CRMWrapperBase w,Guid CrmId) : base(w,CrmStr.salesliteratureitem,CrmId) { }
        public ADNDynSalesliteratureitem(CRMWrapperBase w,InternalDynamicEntity e) : base(w,CrmStr.salesliteratureitem,e,null,null) { }
        public ADNDynSalesliteratureitem(CRMWrapperBase w,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s) : base(w,CrmStr.salesliteratureitem,e,prop,s) { }
        public ADNDynSalesliteratureitem(ResetWrapperHandler fn,string PrimaryFieldValue,bool ForCreate)
            : base(fn,CrmStr.salesliteratureitem)
        {
            if ( PrimaryFieldValue != string.Empty )
                title = PrimaryFieldValue;
        }
        public ADNDynSalesliteratureitem(ResetWrapperHandler fn,Guid CrmId) : base(fn,CrmStr.salesliteratureitem,CrmId) { }
        public ADNDynSalesliteratureitem(ResetWrapperHandler fn,InternalDynamicEntity e) : base(fn,CrmStr.salesliteratureitem,e,null,null) { }
        public ADNDynSalesliteratureitem(ResetWrapperHandler fn,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s) : base(fn,CrmStr.salesliteratureitem,e,prop,s) { }

        /* Properties */

        [CrmDynAttribute(CrmStr.salesliteratureitem)]
        public Guid salesliteratureitemid
        {
            get { return KeyPropertyValue(CrmStr.salesliteratureitemid); }
            set { AddKeyProperty(CrmStr.salesliteratureitemid,value); }
        }
        protected override void SetCrmIdProperty(Guid crmId)
        {
            if ( !dicProperty.ContainsKey(CrmStr.salesliteratureitemid) || KeyPropertyValue(CrmStr.salesliteratureitemid) != crmId )
                salesliteratureitemid = crmId;
        }
        public override Guid PrimaryKeyGuidValue
        {
            get { return salesliteratureitemid; }
            set { salesliteratureitemid = value; }
        }
        
        public override string PrimaryFieldName
        {
            get { return CrmStr.title; }
            set {}
        }

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return title;
            }
            set
            {
                title =   value;
            }
        }

        [CrmDynAttribute(CrmStr.salesliteratureitem)]
        public string wabstract
        {
            get { return StringPropertyValue(CrmStr.wabstract); }
            set { AddStringProperty(CrmStr.wabstract,value); }
        }

        [CrmDynAttribute(CrmStr.salesliteratureitem)]
        public string attacheddocumenturl
        {
            get { return StringPropertyValue(CrmStr.attacheddocumenturl); }
            set { AddStringProperty(CrmStr.attacheddocumenturl,value); }
        }

        [CrmDynAttribute(CrmStr.salesliteratureitem)]
        public string authorname
        {
            get { return StringPropertyValue(CrmStr.authorname); }
            set { AddStringProperty(CrmStr.authorname,value); }
        }

        [CrmDynAttribute(CrmStr.salesliteratureitem)]
        public string documentbody
        {
            get { return StringPropertyValue(CrmStr.documentbody); }
            set { AddStringProperty(CrmStr.documentbody,value); }
        }

        [CrmDynAttribute(CrmStr.salesliteratureitem)]
        public int filetypecode
        {
            get { return PicklistPropertyIntValue(CrmStr.filetypecode); }
            set { AddPicklistProperty(CrmStr.filetypecode,value); }
        }

        public string filetypecodeLabel
        {
            get { return PicklistPropertyLabelValue(CrmStr.filetypecode); }
        }

        [CrmDynAttribute(CrmStr.salesliteratureitem)]
        public bool iscustomerviewable
        {
            get { return BooleanPropertyValue(CrmStr.iscustomerviewable); }
            set { AddBooleanProperty(CrmStr.iscustomerviewable,value); }
        }

        [CrmDynAttribute(CrmStr.salesliteratureitem)]
        public string keywords
        {
            get { return StringPropertyValue(CrmStr.keywords); }
            set { AddStringProperty(CrmStr.keywords,value); }
        }

        [CrmDynAttribute(CrmStr.salesliteratureitem)]
        public override Guid organizationid
        {
            get { return UniqueIdentifierPropertyValue(CrmStr.organizationid); }
            //set { AddUniqueIdentifierProperty(CrmStr.organizationid,value); }
        }

        [CrmDynAttribute(CrmStr.salesliteratureitem)]
        public Guid salesliteratureid
        {
            get { return LookupPropertyValue(CrmStr.salesliteratureid); }
            set { AddLookupProperty(CrmStr.salesliteratureid,CrmStr.salesliterature,value); }
        }

        public string salesliteratureName
        {
            get { return LookupPropertyName(CrmStr.salesliteratureid); }
        }

        [CrmDynAttribute(CrmStr.salesliteratureitem)]
        public string title
        {
            get { return StringPropertyValue(CrmStr.title); }
            set { AddStringProperty(CrmStr.title,value); }
        }

        #region Utilities

        /// <summary>
        /// Return the parent salesliterature
        /// </summary>
        public ADNDynSalesLiterature GetParent(ResetWrapperHandler fn)
        {
            if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine(string.Format(CultureInfo.InvariantCulture,"[V]ADNDynSalesliteratureitem GetParent BEG salesliteratureid: {0}.",salesliteratureid),CRMDynTraceSwitch.Sw.Info);
            ADNDynSalesLiterature dyn   =   DynEntity.GetFromCache<ADNDynSalesLiterature>(CrmStr.salesliterature,wrapper,fn,salesliteratureid);
            if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine(string.Format(CultureInfo.InvariantCulture,"[V]ADNDynSalesliteratureitem GetParent END found: {0}.",dyn.Retrieved),CRMDynTraceSwitch.Sw.Info);
            return dyn;
        }
          
        static string[] ExsalesliteratureitemAttributes     =   new string[]{CrmStr.salesliteratureitemid,
                                                                CrmStr.title,
                                                                CrmStr.wabstract,
                                                                CrmStr.authorname,
                                                                CrmStr.keywords,
                                                                CrmStr.iscustomerviewable,
                                                                CrmStr.attacheddocumenturl,
                                                                CrmStr.mimetype,
                                                                CrmStr.filetypecode,
                                                                CrmStr.filename,
                                                                CrmStr.filesize,
                                                                CrmStr.salesliteratureid,
                                                                CrmStr.organizationid,
                                                                CrmStr.importsequencenumber,
                                                                CrmStr.modifiedby,
                                                                CrmStr.modifiedon,
                                                                CrmStr.createdby,
                                                                CrmStr.createdon
                                                            };
        static string[] salesliteratureitemAttributes       =   new string[]{CrmStr.salesliteratureitemid,
                                                                CrmStr.title,
                                                                CrmStr.wabstract,
                                                                CrmStr.authorname,
                                                                CrmStr.filename,
                                                                CrmStr.filesize,
                                                                CrmStr.salesliteratureid
                                                            };

        /// <summary>
        /// Build a DataTable to display in in RadGrid
        /// </summary>
        static public DataTable ReadForGrid(CRMWrapperBase wrapper,ResetWrapperHandler fn,bool extendedView)
        {
            if (CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynSalesliteratureitem ReadForGrid BEG extendedView: {0}.",extendedView),CRMDynTraceSwitch.Sw.Info);
            DataTable Tb                                        =   null;
            try
            {
                InternalQueryExpression qe                      =   new InternalQueryExpression();
                qe.EntityName                                   =   CrmStr.salesliteratureitem;
                InternalColumnSet RequestCols                   =   new InternalColumnSet();
                InternalFilterExpression fi                     =   new InternalFilterExpression();
                InternalConditionExpression ex                  =   new InternalConditionExpression();
                if ( extendedView )
                        RequestCols.Attributes                  =   ExsalesliteratureitemAttributes;
                else    RequestCols.Attributes                  =   salesliteratureitemAttributes;
                ex.AttributeName                                =   CrmStr.filename;
                ex.Operator                                     =   InternalConditionOperator.NotNull;
                ex.Values                                       =   new object[0];
                fi.Conditions                                   =   new InternalConditionExpression[]{ex};
                qe.ColumnSet                                    =   RequestCols;
                qe.Criteria                                     =   fi;
                GenericDynQueryMultiple<ADNDynSalesliteratureitem> allEntQuery      =   new GenericDynQueryMultiple<ADNDynSalesliteratureitem>(wrapper,fn,CrmStr.salesliteratureitem);
                Tb                                              =   allEntQuery.retrieveDatatable(qe);
            }
            catch (Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynSalesliteratureitem ReadForGrid ex: {0}", ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
            if (CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynSalesliteratureitem ReadForGrid END Count: {0}",Tb.Rows.Count),CRMDynTraceSwitch.Sw.Info);
            return Tb;
        }

        /// <summary>
        /// Build a SortedList 
        /// </summary>
        static public SortedList<Guid,ADNDynSalesliteratureitem>  ReadAll(CRMWrapperBase wrapper,ResetWrapperHandler fn,bool restrict2CustomerView)
        {
            if (CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine(string.Format(CultureInfo.InvariantCulture,"[V]ADNDynSalesliteratureitem ReadAll BEG restrict2CustomerView: {0}.",restrict2CustomerView),CRMDynTraceSwitch.Sw.Info);
            SortedList<Guid,ADNDynSalesliteratureitem> Lst      =   null;
            try
            {
                InternalQueryExpression qe                      =   new InternalQueryExpression();
                qe.EntityName                                   =   CrmStr.salesliteratureitem;
                InternalFilterExpression fi                     =   new InternalFilterExpression();
                InternalConditionExpression ex                  =   new InternalConditionExpression();
                ex.AttributeName                                =   CrmStr.filename;
                ex.Operator                                     =   InternalConditionOperator.NotNull;
                ex.Values                                       =   new object[0];
                if ( restrict2CustomerView )
                {
                    InternalLinkEntity ln                       =   new InternalLinkEntity();
                    ln.LinkFromEntityName                       =   CrmStr.salesliteratureitem;
                    ln.LinkFromAttributeName                    =   CrmStr.salesliteratureid;
                    ln.LinkToEntityName                         =   CrmStr.salesliterature;
                    ln.LinkToAttributeName                      =   CrmStr.salesliteratureid;
                    ln.LinkCriteria                             =   new InternalFilterExpression();
                    InternalConditionExpression ex2             =   new InternalConditionExpression();
                    ex2.AttributeName                           =   CrmStr.iscustomerviewable;
                    ex2.Operator                                =   InternalConditionOperator.Equal;
                    ex2.Values                                  =   new string[]{"true"};
                    ln.LinkCriteria.Conditions                  =   new InternalConditionExpression[]{ex2};
                    qe.LinkEntities                             =   new InternalLinkEntity[]{ln};
                }
                fi.Conditions                                   =   new InternalConditionExpression[]{ex};
                qe.ColumnSet                                    =   new InternalAllColumns();
                qe.Criteria                                     =   fi;
                GenericDynQueryMultiple<ADNDynSalesliteratureitem> allEntQuery      =   new GenericDynQueryMultiple<ADNDynSalesliteratureitem>(wrapper,fn,CrmStr.salesliteratureitem);
                Lst                                             =   allEntQuery.retrieveSortedList(qe);
            }
            catch (Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynSalesliteratureitem ReadAll ex: {0}", ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
            if (CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynSalesliteratureitem ReadAll END Count: {0}",Lst.Count),CRMDynTraceSwitch.Sw.Info);
            return Lst;
        }

        #endregion

    }
}